﻿String.prototype.escapeRegExp = function () {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};
String.prototype.trimEnd = function (c) {
    if (c)
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function (c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}


$(function () {
    var js = $('[data-js]');
    js.filter('[data-js="tab"]').tabs(".js_tabcnt > div");
    $('.js-copy').click(function () {
        var $this = $(this);

    });
    $('.datepicker:text').datetimepicker({
        showOn: "button",
        buttonImage: "/Themes/CoralAdmin/_misc/images/datepicker.jpg",
        buttonImageOnly: true,
        dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        timeFormat: "HH:mm",
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        changeYear: true,
        closeText: "Close"
    });
    $(".ztreeContainer").each(function () {
        var $this = $(this),
            $text = $this.find("input[type='text']"),
            $hidden = $this.find("input[type='hidden']"),
            data = $this.find(".js_ztree_data").val(),
            $ztree = $this.find(".ztree");
        $this.find(".js_ztree_data").hide();

        $ztree.zTreeInit({
            trigger: $this.find(".js_ztree_trigger"), checkFun: function (treeNode) {
                $text.val(treeNode.name);
                $hidden.val(treeNode.id);

            },
            data: data
        })
    });
    js.filter("[data-js='slideswitch']").each(function () {
        $(this).find(".switch_tab .btn").click(function () {
            var $this = $(this), $cnt = $this.parent().next(".switch_cnt") || $this.parent().next().children(".switch_cnt");
            if ($cnt.is(":hidden")) {
                $this.find(".ico").removeClass("ico_slideswitch_show").addClass("ico_slideswitch_hide");
                //$cnt.siblings(".switch_cnt:visible").prev().find(".btn").trigger("click");
                $cnt.slideDown();
            }
            else {
                $this.find(".ico").removeClass("ico_slideswitch_hide").addClass("ico_slideswitch_show");
                $cnt.slideUp();
            }
        }).first().trigger("click");
    })
    $('#spform,.spform').submit(function (e) {
        e.preventDefault();
        var $this = $(this);
        $.each($('.coralfile'), function (i, n) {
            var fileArray = new Array();
            $.each($(n).children('.up_file'), function (ii, nn) {
                fileArray.push($(nn).val());
                $(n).next("input:hidden").val("[" + fileArray + "]");
            });
        });
        $.each($("#spform input[type='checkbox']"), function (i, n) {
            if ($(n).attr("checked") == "checked") {
                return false;
            } else {
                $(n).after('<input type="hidden"  name="' + $(n).attr("name") + '" value="" />');
                return false;
            }
        });
        $.each($("#spform input[type='checkbox']:not(.js-Synchronous)"), function (i, n) {
            if ($(n).attr("checked") == "checked") {
                return false;
            } else {
                $(n).after('<input type="hidden"  name="' + $(n).attr("name") + '" value="" />');
                return false;
            }
        });
        if ($('#category_title').val() == "") {
            $.alert("请选择分类");
            setTimeout("$('.d-close').trigger('click')", 1000);
        } else {
            var $next = $('.tablist a.current').next();
            if ($next.text() != "" && $next.text() != "SEO设置") {
                var result = false;
                $('.table_form').eq($next.index()).find("input").each(function () {
                    if ($(this).val() != "") {
                        result = true;
                    }
                });
                if (result == false) {
                    e.preventDefault();
                    $next.trigger("click");
                }
            }
            $.post($this.attr("action"), $this.serialize(), function (data) {
                if (data != "") {
                    $.alert("操作成功");
                    setTimeout("$('.d-close').trigger('click')", 1000);
                    setTimeout("ajaxLoadList()", 1000);
                }
            });
        }
    });
})